package com.xayy.ds.controller;

import cn.kawins.base.result.ResultBody;
import cn.kawins.spring.mvc.validator.Group;
import com.github.pagehelper.PageInfo;
import com.xayy.mgt.model.NodeTreeInfo;
import com.xayy.mgt.model.dto.NodeInfoDto;
import com.xayy.mgt.model.vo.NodeTreeCopyInfoVo;
import com.xayy.mgt.model.vo.NodeTreeInfoVo;
import com.xtck.ha.common.core.constant.HttpStatus;
import com.xtck.ha.common.core.utils.poi.ExcelUtil;
import com.xtck.ha.common.core.web.controller.BaseController;
import com.xtck.ha.common.core.web.domain.AjaxResult;
import com.xtck.ha.common.core.web.page.TableDataInfo;
import com.xayy.ds.model.DsNodeTreeInfo;
import com.xayy.ds.service.DsNodeTreeInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 模型列表 controller
 *
 * @ClassName: DsNodeTreeInfoController
 * @Description: 外部访问接口
 * @author liuyuan
 * @date 2025-07-03
 */
@Api(tags = "节点树信息，卫星列表结构以及卫星节点结构管理")
@RestController
@RequestMapping("/api/v1.0/nodeTree")
public class DsNodeTreeInfoController extends BaseController {

    @Autowired
    private DsNodeTreeInfoService dsNodeTreeInfoService;


    @ApiOperation("分页查询节点树信息，卫星列表结构以及卫星节点结构列表")
    @GetMapping("/page")
    public AjaxResult page(DsNodeTreeInfo dsNodeTreeInfo)
    {
        startPage();
        List<DsNodeTreeInfo> list = dsNodeTreeInfoService.selectTList(dsNodeTreeInfo);
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setList(list);
        rspData.setMsg("查询成功");
        rspData.setTotal(dsNodeTreeInfoService.count(dsNodeTreeInfo));
        return AjaxResult.success(rspData);
    }

    @ApiOperation("列表查询节点树信息，卫星列表结构以及卫星节点结构")
    @GetMapping("/list")
    public List<DsNodeTreeInfo> list(DsNodeTreeInfo dsNodeTreeInfo)
    {
        List<DsNodeTreeInfo> list = dsNodeTreeInfoService.selectTList(dsNodeTreeInfo);
        return list;
    }

    @ApiOperation("导出节点树信息，卫星列表结构以及卫星节点结构列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response, DsNodeTreeInfo dsNodeTreeInfo)
    {
        List<DsNodeTreeInfo> list = dsNodeTreeInfoService.selectTList(dsNodeTreeInfo);
        ExcelUtil<DsNodeTreeInfo> util = new ExcelUtil<>(DsNodeTreeInfo.class);
        util.exportExcel(response, list, "样例数据");
    }

    // TODO: 2025/7/4  修改查找的方法 
    @ApiOperation("获取$节点树信息，卫星列表结构以及卫星节点结构详细信息")
    @GetMapping(value = "/info/{nodeId}")
    public AjaxResult getInfo(@PathVariable("nodeId") Long nodeId)
    {
        return success(dsNodeTreeInfoService.selectTById(nodeId));
    }

    @ApiOperation("新增$节点树信息，卫星列表结构以及卫星节点结构")
    @PostMapping
    public AjaxResult add(@RequestBody DsNodeTreeInfo dsNodeTreeInfo)
    {
        return toAjax(dsNodeTreeInfoService.insertT(dsNodeTreeInfo));
    }

    @ApiOperation("修改节点树信息，卫星列表结构以及卫星节点结构")
    @PutMapping
    public AjaxResult update(@RequestBody DsNodeTreeInfo dsNodeTreeInfo)
    {
        int i = dsNodeTreeInfoService.updateT(dsNodeTreeInfo);
        return toAjax(i);
    }

//    @ApiOperation("删除节点树信息，卫星列表结构以及卫星节点结构")
//	@DeleteMapping("/{ids}")
//    public AjaxResult remove(@PathVariable Long[] ids)
//    {
//        return toAjax(dsNodeTreeInfoService.deleteTByIds(ids));
//    }


    @PostMapping("/addOrUpdate")
    @ApiOperation(value = "新增/更新树节点(左侧树)", httpMethod = "POST")
    public ResultBody addOrUpdate(@RequestBody DsNodeTreeInfo nodeTreeInfo) {

        return dsNodeTreeInfoService.addOrUpdate(nodeTreeInfo).body();
    }

    @PostMapping("/copyTreeAndNode")
    @ApiOperation(value = "复制节点", httpMethod = "POST")
    public ResultBody copyTreeAndNode(@Validated(Group.Insert.class) @RequestBody NodeTreeCopyInfoVo nodeTreeCopyInfoVo) {
        return dsNodeTreeInfoService.copyTreeAndNode(nodeTreeCopyInfoVo).body();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除树节点(左侧树)", httpMethod = "DELETE")
    public ResultBody delete(@ApiParam(value = "节点id", required = true) @PathVariable Long id) {
        return dsNodeTreeInfoService.deleteTreeById(id).body();
    }

    @GetMapping("/getNodeRelation/{nodeId}")
    @ApiOperation(value = "获取节点关联关系", httpMethod = "GET")
    public ResultBody getNodeRelation(@PathVariable Long nodeId) {
        return dsNodeTreeInfoService.getNodeRelation(nodeId).body();
    }

    @GetMapping("/getChildrenNodeTree/{nodeId}")
    @ApiOperation(value = "获取子节点树", httpMethod = "GET")
    public ResultBody getNodeTree(@PathVariable Long nodeId) {
        return dsNodeTreeInfoService.getNodeTree(nodeId).body();
    }

    @GetMapping("/getNodeBaseInfo/{nodeId}")
    @ApiOperation(value = "获取单节点基本信息和参数列表", httpMethod = "GET", response = NodeTreeInfoVo.class)
    public ResultBody getNodeBaseInfo(@PathVariable("nodeId") Long nodeId) {
        return dsNodeTreeInfoService.getNodeBaseInfo(nodeId).body();
    }

    @DeleteMapping("/deleteNodeInfo/{nodeId}")
    @ApiOperation(value = "删除卫星下的所有节点数据（包括表单）", httpMethod = "DELETE")
    public ResultBody deleteNodeInfo(@PathVariable("nodeId") Long nodeId) {
        return dsNodeTreeInfoService.deleteNodeInfo(nodeId).body();
    }

//    @PostMapping("/pageList")
//    @ApiOperation(value = "获取节点配置信息(分页)", httpMethod = "POST", notes = "评估构建体系-根据左侧树id获取右侧数据")
//    public ResultBody pageList(@RequestBody NodeInfoDto nodeInfoDto) {
//        PageInfo<DsNodeTreeInfo> page = dsNodeTreeInfoService.pageList(nodeInfoDto);
//        return ResultBody.success(page);
//    }

    @GetMapping("/getNodeCodeArray")
    @ApiOperation(value = "获取卫星节点编码数组", httpMethod = "GET", notes = "评估构建体系-根据节点id获取卫星节点编码数组")
    public ResultBody getNodeCodeArray(@RequestParam String nodeId) {
        List<List<String>> array =  dsNodeTreeInfoService.getNodeCodeArray(nodeId);
        return ResultBody.success(array);
    }

    @GetMapping("/getAllSatelliteList")
    @ApiOperation(value = "获取所有卫星列表", httpMethod = "GET", notes = "评估构建体系-获取所有卫星列表")
    public ResultBody getAllSatelliteList() {
        List<DsNodeTreeInfo> array =  dsNodeTreeInfoService.getAllSatelliteList();
        return ResultBody.success(array);
    }



}
